package com.bluetooth.phone;

import android.util.Log;
import android.view.DragEvent;
import android.view.View;

import com.bluetooth.phone.multi.BaseViewHolder;
import com.hsae.bluetooth.phones.R;


public class DragListener implements View.OnDragListener {

    private static final String TAG = "DragListener";
    private int positionTo;
    private boolean isDropped = false;
    private boolean hasEnterDrag;

    public DragListener() {
        //创建dragListener，只监听一个
    }

    @Override
    public boolean onDrag(View target, DragEvent event) {
        int action = event.getAction();
        Object[] localStates = (Object[]) event.getLocalState();
        BaseViewHolder holder = (BaseViewHolder) localStates[0];
        View view = holder.itemView;
        BaseAdapter adapter = (BaseAdapter) localStates[1];
        int positionTo = -1;

        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                if (!hasEnterDrag) {
                    Log.e(TAG, "STARTED  ");
                    isDropped = false;
                    hasEnterDrag = true;
                }
                break;
            case DragEvent.ACTION_DROP://落手后
                isDropped = true;
                if (hasEnterDrag) {
                    hasEnterDrag = false;
                    view.setAlpha(1f);
                    if (target.getId() == R.id.sub_RecyclerView) {

                        if (adapter instanceof DialogAdapter) {
                            Log.e(TAG, "sub_RecyclerView DialogAdapter positionTo=" + positionTo);
                            //positionTo = 落在recycleView 的item之外
                            adapter.onDragItemEnter(positionTo);
                            return true;
                        }
                        return true;
                    }
                    if (target.getId() == R.id.item_view_layout && !target.equals(view)) {
                        if (adapter instanceof DialogAdapter) {
                            positionTo = (int) target.getTag();
                            adapter.onDragItemEnter(positionTo);
                            // adapter.onDragItemEnd(positionTo, hasEnterDrag);
                            hasEnterDrag = false;
                            return true;
                        }
                    }
                }

                break;
            case DragEvent.ACTION_DRAG_ENDED:
                if (isDropped) {  //已经执行了ACTION_DRAG
                    return true;
                }
                if (hasEnterDrag) {
                    Log.e(TAG, "松手处理 ");
                    view.setAlpha(1f);
                    hasEnterDrag = false;
                }
                break;
            case DragEvent.ACTION_DRAG_ENTERED://进入目标view
                if (hasEnterDrag) {
                    if (target.getId() == R.id.item_view_layout && !target.equals(view)) {
                        if (adapter instanceof SubAdapter) {
                            positionTo = (int) target.getTag();
                            Log.e(TAG, "SubAdapter itemView, positionTo...==" + positionTo);
                        //    adapter.onDragItemToMove(positionTo);
                            break;
                        }
                        if (adapter instanceof DialogAdapter && target.getParent().equals(view.getParent())) {
                            positionTo = (int) target.getTag();
                            Log.e(TAG, "DialogAdapter itemView, positionTo...==" + positionTo);
                            adapter.onDragItemToMove(positionTo);
                            break;
                        }

                        if (adapter instanceof DialogAdapter && !target.getParent().equals(view.getParent())) {
                            positionTo = (int) target.getTag();
                            Log.e(TAG, "DialogAdapter itemView, positionTo...==" + positionTo);
                            adapter.insert(holder,positionTo);
                            break;
                        }
                    }

                    if (target.getId() == R.id.sub_RecyclerView) {
                        break;
                    }
                }
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                //  Log.d(TAG, "ACTION_DRAG_EXITED: 不需要处理");
            case DragEvent.ACTION_DRAG_LOCATION:
                //Log.d(TAG, "ACTION_DRAG_LOCATION: ");
                break;
            default:
                break;
        }

        return true;

    }

    private String getName(int action){
        switch (action){
            case DragEvent.ACTION_DRAG_ENDED:
                return "拖放操作结束时触发 (无论是否成功放置";
            case DragEvent.ACTION_DRAG_ENTERED :
                return "被拖动对象进入目标视图区域时触发";
            case DragEvent.ACTION_DRAG_EXITED :
                return "被拖动对象离开目标视图区域时触发";
            case DragEvent.ACTION_DRAG_LOCATION:
                return "被拖动对象在目标视图区域内位置变化时触发";
            case DragEvent.ACTION_DRAG_STARTED:
                return "开始";
            case  DragEvent.ACTION_DROP :
                return "松手";
        }
        return "NONE";
    }

}